﻿using Reckoning;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace TestReckoning
{
    
    
    /// <summary>
    ///This is a test class for ReferenceDataFileTest and is intended
    ///to contain all ReferenceDataFileTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ReferenceDataFileTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ReferenceDataFile Constructor
        ///</summary>
        [TestMethod()]
        public void ReferenceDataFileConstructorTest()
        {
            ReferenceDataFile target = new ReferenceDataFile();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for Read
        ///</summary>
        [TestMethod()]
        public void ReadTest()
        {
            ReferenceDataFile target = new ReferenceDataFile(); 
            string path = "reference_data.xml"; 
            Dictionary<string, uint> referenceData = null;             
            uint totalCount = 0;             
            string softwareVersion = string.Empty;             
            bool expected = true; 
            bool actual = target.Read(path, out referenceData, out totalCount, out softwareVersion);
            Assert.AreEqual(expected, actual);
            Assert.IsTrue(softwareVersion == "1.0.0.0");
            Assert.IsTrue(totalCount == 10);
            bool correct = (referenceData.ContainsKey("hello") && referenceData["hello"] == 1 && 
                referenceData.ContainsKey("goodbye") && referenceData["goodbye"] == 9);
            Assert.IsTrue(correct);
        }

        /// <summary>
        ///A test for Read
        ///</summary>
        [TestMethod()]
        public void ReadTest_BadPath()
        {
            ReferenceDataFile target = new ReferenceDataFile();
            string path = null;
            Dictionary<string, uint> referenceData = null;
            uint totalCount = 0;
            string softwareVersion = string.Empty;
            bool expected = false;
            bool actual = target.Read(path, out referenceData, out totalCount, out softwareVersion);
            Assert.AreEqual(expected, actual);
            Assert.IsTrue(referenceData.Count == 0 && totalCount ==0 && softwareVersion == "1.0.0.0");
        }

        /// <summary>
        ///A test for Write
        ///</summary>
        [TestMethod()]
        public void WriteTest()
        {
            ReferenceDataFile target = new ReferenceDataFile(); 
            string path = System.IO.Path.GetTempFileName(); 
            Dictionary<string, uint> referenceData = new Dictionary<string, uint>();
            referenceData.Add("hello", 23);
            bool expected = true; 
            bool actual = target.Write(path, referenceData);
            Assert.AreEqual(expected, actual);
            Assert.IsTrue(System.IO.File.Exists(path));

            uint totalCount = 0;
            string softwareVersion = string.Empty;
            Dictionary<string, uint> referenceDataRead = null;
            bool readOk = target.Read(path, out referenceDataRead, out totalCount, out softwareVersion);
            System.IO.File.Delete(path);
            Assert.IsTrue(readOk);
            Assert.IsTrue(softwareVersion == "1.0.0.0");
            Assert.IsTrue(totalCount == 23);
            bool correct = (referenceData.ContainsKey("hello") && referenceData["hello"] == 23);
            Assert.IsTrue(correct);            
        }
    }
}
